package com.common.www.controller.tabulation;

import com.alibaba.fastjson.JSON;
import org.springframework.boot.base.jpa.service.IBaseService;
import com.common.www.bean.entity.Result;
import com.common.www.bean.entity.tabulation.XfgcSummary;
import com.common.www.controller.base.DefaultCrudController;
import com.common.www.data.NetCode;
import com.common.www.service.project.ProjectService;
import com.common.www.service.tabulation.XfgcSummaryService;
import com.common.www.utils.ParamsUtils;
import com.common.www.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;


@CrossOrigin
@RestController
@RequestMapping(value = {"mgt/xfgcSummary"})
@Api(tags = "MGT 消防工程清单管理接口")
public class MGTXfgcSummaryController extends DefaultCrudController<XfgcSummary, Long> {
    @Autowired
    private XfgcSummaryService service;
    @Autowired
    private ProjectService projectService;


    protected IBaseService getService() {
        return service;
    }


    @ApiOperation("编辑消防工程清单关联信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "json", value = "String", defaultValue = ""),
    })
    @RequestMapping(value = "/saveByJson", method = RequestMethod.POST)
    public Result saveByJson(String json) {
        XfgcSummary item = JSON.parseObject(json, XfgcSummary.class);
        if (item.getId() == null) {
            service.saveAndFlush(item);
        } else {
            // 如果 id 不为空，则从数据库中获取现有对象
            XfgcSummary existing = service.getByIdLogic(item.getId());
            if (existing != null) {
                // 如果存在，则更新现有对象的属性
                BeanUtils.copyProperties(item, existing, "id");
                // 然后保存更新后的对象
                service.saveAndFlush(existing);
            } else {
                // 如果不存在，则保存解析出来的对象
                service.saveAndFlush(item);
            }
        }
        return R.ok(item);
    }


    @ApiOperation("批量删除启用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "String", defaultValue = ""),
            @ApiImplicitParam(name = "operate", value = "1.删除 2.启用", defaultValue = ""),
    })
    @RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
    public Result batchDelete(Long[] ids, Integer operate) {
        if (ParamsUtils.isInputEmpty(ids, operate)) {
            return R.returnCode(NetCode.CODE_INPUT_OMIT);
        }
        for (int i = 0; i < ids.length; i++) {
            XfgcSummary item = service.findOne(ids[i]);
            if (item != null) {
                item.setFlag(0);
                service.save(item);
            }
        }
        return R.ok();
    }


    @ApiOperation(value = "查询消防工程清单关联列表信息", nickname = "", notes = "")
    @ApiImplicitParams(value = {
    })
    @RequestMapping(value = "/getSummaryList", produces = {"application/json"}, method = RequestMethod.POST)
    public Result getSummaryList(String projectId, Long biddingCompanyId, Integer jieDuan) {
        HashMap hashMap = new HashMap();
        hashMap.put("project.id", projectId);
        List<XfgcSummary> summaryList = null;
        if (biddingCompanyId == null){
            Integer[] jieDuanArray = {1};
            hashMap.put("jieDuan_in", jieDuanArray);
            summaryList = service.getListLogic(hashMap, null);
        } else if (biddingCompanyId != null) {
            Integer[] jieDuanArray = {2,3,4,5};
            hashMap.put("jieDuan_in", jieDuanArray);
            hashMap.put("xfgcBiddingCompany.id", biddingCompanyId);
            summaryList = service.getListLogic(hashMap, null);
//            List<XfgcSummary> companySummaryList = service.getListLogic(hashMap, null);
//            for (XfgcSummary summary : companySummaryList) {
//                if (summaryList != null && summaryList.size() > 0) {
//                    summaryList.add(summary);
//                }
//            }
        }
        return R.ok(summaryList);
    }


}

